package com.fr.data.core.db.dml;

import com.fr.base.core.list.IntList;
import com.fr.data.core.db.DBConstants;
import com.fr.data.core.db.DBUtils;
import com.fr.data.core.db.dialect.Dialect;
import com.fr.data.core.db.dialect.DialectFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Where
  implements Fragment
{
  private List columnNameList = new ArrayList();
  private IntList columnTypeList = new IntList();
  private IntList compareTypeList = new IntList();
  private List columnValueList = new ArrayList();
  private IntList relationTypeList = new IntList();

  public void addColumn(String paramString, Object paramObject)
  {
    addColumn(paramString, 12, 0, paramObject, 0);
  }

  public void addColumn(String paramString, int paramInt1, int paramInt2, Object paramObject)
  {
    addColumn(paramString, paramInt1, paramInt2, paramObject, 0);
  }

  public void addColumn(String paramString, int paramInt1, int paramInt2, Object paramObject, int paramInt3)
  {
    if (paramString != null)
    {
      if (getColumnSize() > 0)
        this.relationTypeList.add(paramInt3);
      this.columnNameList.add(paramString);
      this.columnTypeList.add(paramInt1);
      this.compareTypeList.add(paramInt2);
      this.columnValueList.add(paramObject);
    }
  }

  public int getColumnSize()
  {
    return ((this.columnNameList == null) ? 0 : this.columnNameList.size());
  }

  public static boolean validate(Where paramWhere)
  {
    return ((paramWhere != null) && (paramWhere.getColumnSize() > 0));
  }

  public String toStatementSQLString(Dialect paramDialect)
  {
    String str1;
    String str2;
    int l;
    if (paramDialect == null)
      paramDialect = DialectFactory.getDefaultDialect();
    StringBuffer localStringBuffer = new StringBuffer();
    int i = getColumnSize();
    if (i > 0)
    {
      localStringBuffer.append(" WHERE ");
      str1 = "=";
      str2 = "?";
      for (l = 0; l < i; ++l)
      {
        if (l != 0)
        {
          int j = this.relationTypeList.get(l - 1);
          localStringBuffer.append((j == 0) ? " AND " : " OR ");
          int k = this.compareTypeList.get(l);
          String[] arrayOfString = SQLUtils.getOPAndValueString(k, str2);
          str1 = arrayOfString[0];
          str2 = arrayOfString[1];
        }
        localStringBuffer.append(paramDialect.column2SQL((String)this.columnNameList.get(l)));
        localStringBuffer.append(str1);
        localStringBuffer.append(str2);
      }
    }
    return localStringBuffer.toString();
  }

  public int applyColumnTypeValue(Dialect paramDialect, Connection paramConnection, Table paramTable, PreparedStatement paramPreparedStatement, int paramInt)
    throws SQLException
  {
    if ((paramPreparedStatement == null) || (paramInt <= 0))
      return paramInt;
    int i = this.columnNameList.size();
    for (int j = 0; j < i; ++j)
      DBUtils.applyColumnTypeValue(paramDialect, paramConnection, paramTable, (String)this.columnNameList.get(j), paramPreparedStatement, paramInt + j, this.columnTypeList.get(j), this.columnValueList.get(j));
    return (paramInt + i);
  }

  public String toNormalSQLString(Dialect paramDialect)
  {
    String str1;
    int i1;
    int i3;
    if (paramDialect == null)
      paramDialect = DialectFactory.getDefaultDialect();
    int i = this.columnNameList.size();
    StringBuffer localStringBuffer = new StringBuffer();
    if (i > 0)
    {
      localStringBuffer.append(" WHERE ");
      str1 = "=";
      i1 = 0;
      for (i3 = 0; i3 < i; ++i3)
      {
        int k = this.columnTypeList.get(i3);
        int l = DBUtils.getColumnFRType(k);
        if (l == DBConstants.FR_STREAM)
          break label276:
        String str2 = (String)this.columnValueList.get(i3);
        if (str2.indexOf("'") == -1)
        {
          if (str2.indexOf("\"") != -1)
            break label276:
          int j = (this.relationTypeList.size() > 0) ? this.relationTypeList.get(i3) : 0;
          if (i1 != 0)
            localStringBuffer.append((j == 0) ? " AND " : " OR ");
          i1 = 1;
          int i2 = this.compareTypeList.get(i3);
          String[] arrayOfString = SQLUtils.getOPAndValueString(i2, str2);
          str1 = arrayOfString[0];
          str2 = arrayOfString[1];
          localStringBuffer.append(paramDialect.column2SQL((String)this.columnNameList.get(i3)));
          localStringBuffer.append(str1);
          if (l == DBConstants.FR_STRING)
          {
            localStringBuffer.append("'");
            localStringBuffer.append(str2);
            localStringBuffer.append("'");
          }
          else
          {
            localStringBuffer.append(str2);
          }
        }
      }
    }
    label276: return localStringBuffer.toString();
  }

  public void clearAllColumn()
  {
    this.columnNameList.clear();
    this.columnTypeList.clear();
    this.compareTypeList.clear();
    this.columnValueList.clear();
    this.relationTypeList.clear();
  }

  public String toString()
  {
    return toNormalSQLString(null);
  }
}